package com.sztx.worker_monitor.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sztx.se.common.domain.Result;
import com.sztx.se.common.domain.ResultCode;
import com.sztx.se.web.controller.BaseController;
import com.sztx.worker_monitor.web.handle.CommonHandle;
import com.sztx.worker_monitor.web.handle.LoginHandle;
import com.sztx.worker_monitor.web.handle.QuartzHandle;
import com.sztx.worker_monitor.web.handle.SchedNameHandle;
import com.sztx.worker_monitor.web.model.GlobalSeting;
import com.sztx.worker_monitor.web.model.QuartzModel;
import com.sztx.worker_monitor.web.model.ResultStatus;
import com.sztx.worker_monitor.web.model.SearchListModel;

@RequestMapping("/quartz")
@Controller
public class QuartzController extends BaseController {
	

	
	@Autowired(required = false)
	private QuartzHandle quartzHandle;
	
	@Autowired(required = false)
	private SchedNameHandle schedNameHandle;
	


	/** *****************************以下为页面控制****************************************/
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.Index);
        String keyTaskname =  getStringParameter("key_taskname",""); 
        String keySchedname = getStringParameter("key_schedname","");
		List<QuartzModel> quartzModelList = quartzHandle.getQuartzModelList(keyTaskname, keySchedname);
		model.addAttribute("keyTaskname", keyTaskname);
		model.addAttribute("quartzModelList", quartzModelList);
		model.addAttribute("keySchedname", keySchedname);

		return "quartz/index";
	}
	
	@RequestMapping("/addJob")
	public String addJob(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu( model, GlobalSeting.MenuIndex.Index);

		List<String> schedNames = schedNameHandle.getSchedNameList();
		model.addAttribute("schedNames", schedNames);
		return "quartz/addJob";
	}
	
	@RequestMapping("/editJob")
	public String editJob(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.Index);
		//获取一个job的信息

		
		String schedName 	= request.getParameter("schedName");
		String jobName 		= request.getParameter("jobName");
		String jobGroup 	= request.getParameter("jobGroup");
		String triggerName 	= request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		
		QuartzModel quartzModel = quartzHandle.getJobModel(schedName, jobName,jobGroup, triggerName,  triggerGroup);
		if(quartzModel == null)
			return "quartz/addJob";
		
		model.addAttribute("quartzModel", quartzModel);
		return "quartz/editJob";
	}
	
	@RequestMapping(value="/about", method=RequestMethod.GET)
	public String about(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.About);
		return "quartz/about";
	}
	
	@RequestMapping(value="/editSchedName", method=RequestMethod.GET)
	public String editSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.Index);

		List<String> schedNames = schedNameHandle.getSchedNameList();
		model.addAttribute("schedNames", schedNames);
		if(LoginHandle.checkLogin() >=2)
			return "quartz/editSchedName";
		else
			return "quartz/index";
			
	}
	
	/*******************************以上为页面控制*****************************************/
	
	
	/******************************以下是操作控制*****************************************/
	@RequestMapping(value="/restartjob", method=RequestMethod.POST)
	public Result restartjob(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String schedName 	= request.getParameter("schedName");
		String triggerName = request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		
		String jobName = request.getParameter("jobName");
		String jobGroup = request.getParameter("jobGroup");
		
		String errDes = quartzHandle.ctrlJob(
				QuartzHandle.MethodType.Restart,
				schedName,
				triggerName,
				triggerGroup,
				jobName,
				jobGroup);
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	
	/**
	 * 删除一个job
	 * 
	 * **/
	@RequestMapping(value="/deleteJob", method=RequestMethod.POST)
	public Result deleteJob(HttpServletRequest request, HttpServletResponse response, Model model) {

		String schedName 	= request.getParameter("schedName");
		String triggerName = request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		
		String jobName = request.getParameter("jobName");
		String jobGroup = request.getParameter("jobGroup");
		
		
		String errDes = quartzHandle.ctrlJob(
				QuartzHandle.MethodType.Delete,
				schedName,
				triggerName,
				triggerGroup,
				jobName,
				jobGroup);
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	
	/**
	 * 暂停一个job
	 * 
	 * */
	@RequestMapping(value="/pauseJob", method=RequestMethod.POST)
	public Result pauseJob(HttpServletRequest request, HttpServletResponse response, Model model) {

		String schedName 	= request.getParameter("schedName");
		String triggerName = request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		
		String jobName = request.getParameter("jobName");
		String jobGroup = request.getParameter("jobGroup");
		

		String errDes = quartzHandle.ctrlJob(
				QuartzHandle.MethodType.Pause,
				schedName,
				triggerName,
				triggerGroup,
				jobName,
				jobGroup);
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	/**
	 * 添加一个job
	 * 
	 * */
	@RequestMapping(value="/addNewJob", method=RequestMethod.POST)
	public Result addNewJob(HttpServletRequest request, HttpServletResponse response, Model model) {

		String schedName 	= request.getParameter("schedName");
		String jobName 		= request.getParameter("jobName");
		String jobGroup 	= request.getParameter("jobGroup");
		String jobClassName = request.getParameter("jobClassName");
		String description 	= request.getParameter("description");
		String isRecoveryStr= request.getParameter("isRecovery");
		
		String triggerName 	= request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		String isCronTriggerStr = request.getParameter("isCronTrigger");   //非必须，默认为true
		String expression 	= request.getParameter("expression");   	
		

		String errDes = quartzHandle.addNewJob(	 
				 schedName,
				 jobName,
				 jobGroup,
				 jobClassName,
				 description,
				 isRecoveryStr,
				 triggerName,
				 triggerGroup,
				 isCronTriggerStr,
				 expression );
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	
	/**
	 * 更新一个job
	 * 
	 * */
	@RequestMapping(value="/updateJob", method=RequestMethod.POST)
	public Result updateJob(HttpServletRequest request, HttpServletResponse response, Model model) {

		String schedName 	= request.getParameter("schedName");
		String jobName 		= request.getParameter("jobName");
		String jobGroup 	= request.getParameter("jobGroup");
		String jobClassName = request.getParameter("jobClassName");
		String description 	= request.getParameter("description");
		String isRecoveryStr= request.getParameter("isRecovery");
		
		String triggerName 	= request.getParameter("triggerName");
		String triggerGroup = request.getParameter("triggerGroup");
		String isCronTriggerStr = request.getParameter("isCronTrigger");   //非必须，默认为true
		String expression 	= request.getParameter("expression");   	
		
		
		String errDes = quartzHandle.updateJob(
				schedName,
				jobName,
				jobGroup,
			    jobClassName,
				description,
				isRecoveryStr,
				triggerName,
				triggerGroup,
				isCronTriggerStr,
				expression);
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	

	/**
	 * 获取所有的 jobname  和schedulname
	 * 
	 * */
	@RequestMapping(value="/getSearchList", method=RequestMethod.POST)
	public Result getSearchList(HttpServletRequest request, HttpServletResponse response, Model model) {


		SearchListModel searchListModel = quartzHandle.getSearchList();
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("searchListModel", searchListModel);
		return result;
	}
	
	/**
	 * 获取详细信息
	 * 
	 * */
	@RequestMapping(value="/getJobDetail", method=RequestMethod.POST)
	public Result getJobDetail(HttpServletRequest request, HttpServletResponse response, Model model) {

			String schedName 	= request.getParameter("schedName");
			String triggerName = request.getParameter("triggerName");
			String triggerGroup = request.getParameter("triggerGroup");
			
			String jobName = request.getParameter("jobName");
			String jobGroup = request.getParameter("jobGroup");
			

			ResultStatus resultStatus = quartzHandle.getJobDetail(
					schedName,
					jobName,
					jobGroup,
					triggerName,
					triggerGroup);
		 
		 if(!resultStatus.getbSuccess()){
			 
			Result result = new Result(ResultCode.COMMON_BUSINESS_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		 }
		 
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("jobDetail", resultStatus.getRevert());
		return result;
	}
	
	/******************************以上是操作控制*****************************************/

	/**
	 * 解析一个expression
	 * ****/
	@RequestMapping(value="/showExDes", method=RequestMethod.POST)
	public Result showExDes(HttpServletRequest request, HttpServletResponse response, Model model) {


		String exdes = CommonHandle.getExDes(getStringParameter("expression"));
		
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("exdes", exdes);
		return result;
	}
	
	/**
	 * 删除一个schedname
	 * */
	@RequestMapping(value="/delSchedName", method=RequestMethod.POST)
	public Result delSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {

		String errDes = schedNameHandle.delSchedName(getStringParameter("schedName"));
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	/**
	 * 添加一个schedname
	 * */
	@RequestMapping(value="/addSchedName", method=RequestMethod.POST)
	public Result addSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {

		String errDes = schedNameHandle.addSchedName(getStringParameter("schedName"));
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
}
